package com.zlc.platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.zlc.common.common.entity.PageVo;
import com.zlc.common.core.user.mapper.MayanPlatUserAgentMapper;
import com.zlc.common.core.user.model.vo.MayanPlatUserAgentVo;
import com.zlc.common.core.user.service.MayanPlatUserAgentService;
import com.zlc.common.model.user.UserAgentRequest;
import com.zlc.common.util.AssertUtils;
import com.zlc.common.util.UserHelper;
import com.zlc.platform.service.UserAgentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Slf4j
@Service
public class UserAgentServiceImpl implements UserAgentService {

    @Autowired
    private MayanPlatUserAgentService mayanPlatUserAgentService;
    @Autowired
    private MayanPlatUserAgentMapper mayanPlatUserAgentMapper;

    @Override
    public PageVo<MayanPlatUserAgentVo> queryAgentList(UserAgentRequest request) {
        PageHelper.startPage(request.getPageNum(),request.getPageSize());
        List<MayanPlatUserAgentVo> page = mayanPlatUserAgentMapper.queryUserAgentList(request);
        return PageVo.page(page);
    }

    @Override
    public void addAgent(MayanPlatUserAgentVo agentVo) {
        AssertUtils.isTrue(!ObjectUtils.isEmpty(agentVo.getAgentUserAccount()),"代理用户账号不可为空");
        AssertUtils.isTrue(!ObjectUtils.isEmpty(agentVo.getAgentStartTime()),"代理开始时间不可为空");
        AssertUtils.isTrue(!ObjectUtils.isEmpty(agentVo.getAgentEndTime()),"代理结束时间不可为空");
        agentVo.setAgentStatus("ACTIVE");
        agentVo.setUserAccount(UserHelper.getNowAccount());
        mayanPlatUserAgentService.saveVo(agentVo);
    }

    @Override
    public void updateAgent(MayanPlatUserAgentVo agentVo) {
        AssertUtils.isTrue(!ObjectUtils.isEmpty(agentVo.getAgentUserAccount()),"代理用户账号不可为空");
        AssertUtils.isTrue(!ObjectUtils.isEmpty(agentVo.getAgentStartTime()),"代理开始时间不可为空");
        AssertUtils.isTrue(!ObjectUtils.isEmpty(agentVo.getAgentEndTime()),"代理结束时间不可为空");
        agentVo.setUserAccount(UserHelper.getNowAccount());
        mayanPlatUserAgentService.saveVo(agentVo);
    }

    @Override
    public void removeAgent(String id) {
        mayanPlatUserAgentService.deleteById(id);
    }
}
